let content=""
Page({
  data: {
    z_URL:"cloud://test-hf5g2.7465-test-hf5g2-1304459590/image/植物图片/",
    y_URL:"cloud://test-hf5g2.7465-test-hf5g2-1304459590/image/药物图片/",
    z_imgURL:"",
    y_imgURL:"",
    notice:false,
    showIndex:true,
    showInput:false,
    preview:false,
    pre_index:0,
    rembNum:0,
    number:0,
    reviewNum:0,
    index:0,
    testId:'asy',
    data_index:0,  // 题目序列
    reciteList:[],//背药序列
    yesList: [], // 认识的序列
    notSureList: [], // 不确定的序列
    noList:[],//不认识的序列
    counter:0
  },

  onLoad: function (options) {
    // wx.enableAlertBeforeUnload(){

    // }
    let that=this;
    console.log(options)
    wx.setNavigationBarTitle("专业背诵") // 动态设置导航条标题
    that.setData({
      rembNum:options.number-options.data_index,
      data_index:options.data_index,
      number:options.number,
      reviewNum:options.reviewNum,
      pre_index:options.pre_index
    })
    if(that.data.reviewNum!=0){
      let reviewNum=Number(that.data.reviewNum)
      let pre_index=Number(that.data.data_index)-Number(reviewNum)
      this.data.pre_index=pre_index
      wx.cloud.database().collection("Dictionary").skip(pre_index).limit(reviewNum).get()
      .then(res=>{
        console.log(res)
          that.setData({
            preview:true,
            reciteList:res.data,
            z_imgURL:that.data.z_URL+res.data[that.data.index].chapter+"/"+res.data[that.data.index].section+"/"+res.data[that.data.index].name+".jpg",
            y_imgURL:that.data.y_URL+res.data[that.data.index].chapter+"/"+res.data[that.data.index].section+"/"+res.data[that.data.index].name+".jpg"
          })
      }) 
      .catch(res=>{
        console.log(res)
      })
    }else{
      let rembNum=Number(that.data.rembNum)
      let data_index=Number(that.data.data_index)
      wx.cloud.database().collection("Dictionary").skip(data_index).limit(rembNum).get()
      .then(res=>{
        console.log(res)
          let reciteList=res.data
          let addItem=wx.getStorageSync('addList')
          console.log(addItem)
          if(wx.getStorageSync('addList')){
            console.log("12354")
            if(addItem.length>0){
              addItem="["+addItem+"]"
              addItem=JSON.parse(addItem)
            }
            this.data.counter=addItem.length
            console.log(addItem)
            reciteList=reciteList.concat(addItem)
            that.setData({
              rembNum:reciteList.length
            })
          }
          that.setData({
            reciteList:reciteList,
            z_imgURL:that.data.z_URL+res.data[that.data.index].chapter+"/"+res.data[that.data.index].section+"/"+res.data[that.data.index].name+".jpg",
            y_imgURL:that.data.y_URL+res.data[that.data.index].chapter+"/"+res.data[that.data.index].section+"/"+res.data[that.data.index].name+".jpg"
          })
      }) 
      .catch(res=>{
        console.log(res)
      })
    }
  },
  onShow(){
  },
  // onUnload(){
  //   let that=this
  //   let openid=wx.getStorageSync('openid');
  //   let recite=[]
  //   let reciteItem={}
  //   reciteItem.data_index=Number(that.data.data_index)+Number(that.data.index)-Number(this.data.counter)
  //   reciteItem.number=Number(that.data.number)
  //   // reciteItem.number=2
  //   reciteItem.pre_index=that.data.pre_index
  //   // recite = Object.assign({},reciteItem);
  //   recite.push(reciteItem)
  //   // test=that.data.questionList.test
  //   console.log(recite)
  //   wx.cloud.callFunction({
  //     name:"updata",
  //     data:{
  //       action:"userList_recite_update",           
  //       openid:openid,
  //       recite:recite
  //     }
  //   }) 
  // },
  // 认识事件
  toYesPage: function (e) {
    let that=this
    if (that.data.index < that.data.reciteList.length) {
      // 渲染下一题
      that.setData({
        showInput:true,
        notice:false
      })
    } 
  },
  // 不认识事件
  toNoPage(){
    let that=this
    that.setData({
      showIndex:false,
      notice:false,
      counter:this.data.counter+1
    })
  },
  toNotice(){
    let that=this
    that.setData({
      notice:true
    })
  },
  nextSubmit(){
    let that=this
    let index=that.data.index
    let number=that.data.number
    let data_index=that.data.data_index
    let addReciteList=this.data.reciteList.concat(this.data.reciteList[index])
    console.log(addReciteList);
    that.setData({
      reciteList:addReciteList
    })
    if (that.data.index < that.data.reciteList.length) {
      // 渲染下一题
      if(that.data.rembNum==0||that.data.reviewNum==0){
        that.setData({
          index: that.data.index + 1,
          showIndex:true,
          z_imgURL:that.data.z_URL+that.data.reciteList[that.data.index+1].chapter+"/"+that.data.reciteList[that.data.index+1].section+"/"+that.data.reciteList[that.data.index+1].name+".jpg",
        y_imgURL:that.data.y_URL+that.data.reciteList[that.data.index+1].chapter+"/"+that.data.reciteList[that.data.index+1].section+"/"+that.data.reciteList[that.data.index+1].name+".jpg"
        })
      }else{
        if(that.data.preview==true){
          that.setData({
            index: that.data.index + 1,
            showIndex:true,
            z_imgURL:that.data.z_URL+that.data.reciteList[that.data.index+1].chapter+"/"+that.data.reciteList[that.data.index+1].section+"/"+that.data.reciteList[that.data.index+1].name+".jpg",
        y_imgURL:that.data.y_URL+that.data.reciteList[that.data.index+1].chapter+"/"+that.data.reciteList[that.data.index+1].section+"/"+that.data.reciteList[that.data.index+1].name+".jpg"
            // reviewNum: that.data.reviewNum-1,
          })
        }else{
          that.setData({
            index: that.data.index + 1,
            showIndex:true,
            z_imgURL:that.data.z_URL+that.data.reciteList[that.data.index+1].chapter+"/"+that.data.reciteList[that.data.index+1].section+"/"+that.data.reciteList[that.data.index+1].name+".jpg",
            y_imgURL:that.data.y_URL+that.data.reciteList[that.data.index+1].chapter+"/"+that.data.reciteList[that.data.index+1].section+"/"+that.data.reciteList[that.data.index+1].name+".jpg"
          })
        }
      }
      // if(that.data.index==that.data.reciteList.length)
      // {
      //   that.setData({
      //     data_index:Number(data_index)
      //   })
      //   // data_index=index+data_index+1
      //   wx.redirectTo({
      //     url: '../results_tcm/results_tcm?data_index='+data_index+'&number='+number,
      //   })
      // }
    }
    
  },
  getValue(e){
    content=e.detail.value
  },
  submitButton(){
    var that=this
    var reciteList=that.data.reciteList
    var index=that.data.index
    var data_index=that.data.data_index
    var number=that.data.number
    if(reciteList[index].name==content)
    {
      wx.showToast({
        title: '回答正确',
      })
      if (that.data.index < that.data.reciteList.length-1) {
        // 渲染下一题
        console.log("11111111")
        if(that.data.preview==false){
          that.setData({
            index: that.data.index + 1,
            showIndex:true,
            showInput:false,
            rembNum: that.data.rembNum-1,
            z_imgURL:that.data.z_URL+that.data.reciteList[that.data.index+1].chapter+"/"+that.data.reciteList[that.data.index+1].section+"/"+that.data.reciteList[that.data.index+1].name+".jpg",
            y_imgURL:that.data.y_URL+that.data.reciteList[that.data.index+1].chapter+"/"+that.data.reciteList[that.data.index+1].section+"/"+that.data.reciteList[that.data.index+1].name+".jpg"
          })
        }else{
          that.setData({
            index: that.data.index + 1,
            showIndex:true,
            showInput:false,
            reviewNum: that.data.reviewNum-1,
            z_imgURL:that.data.z_URL+that.data.reciteList[that.data.index+1].chapter+"/"+that.data.reciteList[that.data.index+1].section+"/"+that.data.reciteList[that.data.index+1].name+".jpg",
            y_imgURL:that.data.y_URL+that.data.reciteList[that.data.index+1].chapter+"/"+that.data.reciteList[that.data.index+1].section+"/"+that.data.reciteList[that.data.index+1].name+".jpg"
          })
        }
      }
      else if(that.data.index==that.data.reciteList.length-1&&that.data.preview==false)
        {
          console.log("22222222")
          that.data.data_index=that.data.number;
          wx.redirectTo({
            url: '../results_tcm/results_tcm?data_index='+data_index+'&number='+number+'&pre_index='+this.data.pre_index,
          })
        }else if(that.data.index==that.data.reciteList.length-1&&that.data.preview==true){
          console.log("333333333")
          that.data.preview=false;
          that.data.pre_index=that.data.data_index;
          let rembNum=Number(that.data.rembNum)
          let data_index=Number(that.data.data_index)
          wx.cloud.database().collection("Dictionary").skip(data_index).limit(rembNum).get()
          .then(res=>{
            let reciteList=res.data
            let addItem=wx.getStorageSync('addList')
            console.log(addItem)
            if(wx.getStorageSync('addList')){
              console.log("12354")
              addItem="["+addItem+"]"
              addItem=JSON.parse(addItem)
              console.log(addItem)
              reciteList=reciteList.concat(addItem)
              that.setData({
                rembNum:reciteList.length
              })
            }
              that.setData({
                index:0,
                showIndex:true,
                showInput:false,
                reciteList:reciteList,
                reviewNum: that.data.reviewNum-1,
                z_imgURL:that.data.z_URL+res.data[that.data.index].chapter+"/"+res.data[that.data.index].section+"/"+res.data[that.data.index].name+".jpg",
                y_imgURL:that.data.y_URL+res.data[that.data.index].chapter+"/"+res.data[that.data.index].section+"/"+res.data[that.data.index].name+".jpg"
              })
          }) 
          .catch(res=>{
            console.log(res)
          })
        }
    }
    else
    {
      wx.showToast({
        title: '回答错误',
        icon:"none"
      })
      that.setData({
        showIndex:false,
        showInput:false
      })
    }
  },
  toNoButton()
  {
    let that=this
    that.setData({
      showIndex:false,
      showInput:false
    })
  },
  z_exchangeImage(e){
    let that=this
    let URL=that.data.z_imgURL.split('.')
    URL[2]="webp"
    URL=URL.join('.')
    console.log(e)
    that.setData({
      z_imgURL:URL
    })
  },
  y_exchangeImage(e){
    let that=this
    let URL=that.data.y_imgURL.split('.')
    URL[2]="webp"
    URL=URL.join('.')
    console.log(e)
    that.setData({
      y_imgURL:URL
    })
  }
})
